/*
 * @Author: 王庆庆
 * @Date: 2024-03-29 13:51:37
 * @LastEditors: 王庆庆
 * @LastEditTime: 2024-11-16 18:05:19
 * @Description:
 *
 */
import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

const urlToPath = (url) => {
	return fileURLToPath(new URL(url, import.meta.url));
};
// https://vitejs.dev/config/
export default defineConfig({
	base: "./",
	minify: true, //压缩
	sourceMap: false,
	productionSourceMap: false,
	plugins: [
		vue(),
	],
	css: {
		preprocessorOptions: {
			scss: {
				api: "modern-compiler", // or 'modern'
			},
		},
	},
	resolve: {
		alias: {
			"@": urlToPath("./src"),
			"@public": urlToPath("public"),
			"@comp": urlToPath("src/components"),
			"@libs": urlToPath("src/libs"),
			"@image": urlToPath("src/assets/image"),
			"@hooks": urlToPath("src/hooks"),
		},
	},
	server: {
		host: "0.0.0.0",
		port: 8090,
		open: true,
	},
	build: {
		rollupOptions: {
			output: {
				// 入口文件输出配置
				entryFileNames: `js/[name]-[hash].js`,
				// 代码分割后的文件输出配置
				chunkFileNames: `js/[name]-[hash].js`,
				// 静态资源输出配置
				assetFileNames(assetInfo) {
					// css文件单独输出到css文件夹
					if (assetInfo.name.endsWith(".css")) {
						return `css/[name]-[hash].css`;
					}
					// 图片文件单独输出到img文件夹
					else if (
						[".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp"].some(
							(ext) => assetInfo.name.endsWith(ext)
						)
					) {
						return `img/[name]-[hash].[ext]`;
					}
					// 其他资源输出到assets文件夹
					else {
						return `assets/[name]-[hash].[ext]`;
					}
				},
			},
		},
	},
});
